/*
 * BindStatusMap.java
 *
 * Copyright 2010 Jack Leow
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package my.edu.clhs.springframework.web.servlet.view.stringtemplate3.support;

import java.util.AbstractMap;
import java.util.Set;

import org.springframework.web.servlet.support.RequestContext;

/**
 * Class that allows BindStatus instances to be accessed via a Map interface so
 * as to be accessible as part of the StringTemplate model.
 *
 * This is an immutable map that does not allow key/value iteration.
 * More specifically, only the {@link #containsKey(Object)} and
 * {@link #get(Object)} methods are implemented. All other {@link java.util.Map}
 * methods throw <tt>UnsupportedOperationException</tt>.
 *
 * @author Jack Leow
 * @version 1.0
 * @since 1.0 (Mar 25, 2010, 8:17:15 PM)
 */
public class BindStatusMap extends AbstractMap {
    private final RequestContext requestContext;
    
    public BindStatusMap(RequestContext requestContext) {
        this.requestContext = requestContext;
    }
    
    /**
     * This implementation always throws <tt>UnsupportedOperationException</tt>.
     */
    // @Override
    public Set entrySet() {
        throw new UnsupportedOperationException("not implemented");
    }
    
    // TODO document and cache
    // @Override
    public boolean containsKey(Object key) {
        try {
            requestContext.getBindStatus((String)key);
            
            return true;
        } catch (IllegalStateException e) {
            return false;
        }
    }
    
    // @Override
    public Object get(Object key) {
        try {
            String path = (String)key;
            return new CustomBindStatus(requestContext, path);
        } catch (IllegalStateException e) {
            return null;
        }
    }
}